/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package es.fpsampayo.recursos;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author marcos
 */
public class Conexion
{

    private String url = null;
    private String baseDatos = null;
    private String usuario = "pruebas";
    private String contraseña = "1234";

    private Connection conexion;
    private Statement stm;
    
    public Conexion ()
    {
        Configuracion xml = new Configuracion();
        try {
            String host = xml.leeConfig("host");
            String port = xml.leeConfig("port");
            String ddbb = xml.leeConfig("ddbb");
            url = "jdbc:postgresql://" + host + ":" + port;
            baseDatos = ddbb;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        Conectar();
    }
    
    public Conexion (String url, String baseDatos){

        this.url = url;
        this.baseDatos = baseDatos;
        Conectar();
    }

    public Conexion (String url, String baseDatos, String usuario, String contraseña){

        this.url = url;
        this.baseDatos = baseDatos;
        this.usuario = usuario;
        this.contraseña = contraseña;
        Conectar();
    }

    public final void Conectar()
    {
        try
        {
            Class.forName("org.postgresql.Driver");
            conexion = DriverManager.getConnection(url + "/" + baseDatos, usuario, contraseña);
            stm = conexion.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        }
        catch (SQLException ex)
        {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (ClassNotFoundException ex)
        {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Desconectar()
    {
        try
        {
            if (stm != null)
            {
                stm.close();
            }
            if (conexion != null)
            {
                conexion.close();
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ResultSet Select(String procedimiento)
    {
        ResultSet rs = null;
        if (conexion != null && stm != null)
        try
        {
            rs = stm.executeQuery(procedimiento);
        }
        catch (SQLException ex)
        {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }

    public int insertar (String sql)
    {
        int nReg = 0;
        try {
            nReg = stm.executeUpdate(sql);
        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nReg;
    }
    
    public int insertarPorLotes (String sql)
    {
        int nReg = 0;
        try {
            stm.addBatch(sql);
        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nReg;
    }
    
    public int ejecutarLote (){
        
        int nReg = 0;
        try {
            stm.executeBatch();
        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return nReg;
    }
    
    public void limpiarLote(){
        try {
            stm.clearBatch();
        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}